<?php

namespace app\bll;

use Exception;
use support\Db;

class UcenterIndexLayout
{
    /**
     * 保存布局
     *
     * @param array $data
     * @throws Exception
     * @return void
     */
    public function setLayout(array $data)
    {

        Db::beginTransaction();
        try {
            Db::table('frame_ucenter_index_layout')
                ->delete();
            Db::table('frame_ucenter_index_layout')
                ->insert($data);
            Db::commit();
        } catch (Exception $ex) {
            Db::rollBack();
            throw $ex;
        }
    }

    /**
     * 获取布局
     *
     * @return array
     */
    public function getLayout(): array
    {
        $layouts = Db::table('frame_ucenter_index_layout')
            ->get()
            ->map(function ($value) {
                return (array)$value;
            })
            ->toArray();
        $parseLayouts = [];
        foreach ($layouts as $key => $layout) {
            $parseLayouts[$layout['areaid']][] = $layout;
        }
        return $parseLayouts;
    }
}
